package com.gitee.kinbug.quickstart.aid;

import java.io.Serializable;
import com.alibaba.fastjson.JSON;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author kinbug
 * RESTful API 返回类型
 * Created at 2018/3/8.
 */
@Data
@NoArgsConstructor
public class ResultJson<T> implements Serializable{

    private static final long serialVersionUID = 783015033603078674L;
    private int code;
    private String msg;
    private T data;
    
    public static ResultJson<Object> SUCCESS = new ResultJson<Object>(ResultCode.SUCCESS);
    public static ResultJson<Object> ERROR = new ResultJson<Object>(ResultCode.SERVER_ERROR);

    public ResultJson<T> success(T data) {
    	return new ResultJson<T>(ResultCode.SUCCESS, data);
    }
    
    public ResultJson<T> error(ResultCode resultCode,T data) {
    	return  new ResultJson<T>(resultCode,data);
    }
    
    public ResultJson<T> error(ResultCode resultCode) {
    	return  new ResultJson<T>(resultCode);
    }
    
    public ResultJson (ResultCode resultCode) {
        setResultCode(resultCode);
    }

    public ResultJson (ResultCode resultCode,T data) {
        setResultCode(resultCode);
        this.data = data;
    }

    public void setResultCode(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
